﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ATW.MT.CommonClass;
using ATW.MT.BUS;
using System.Data;
using System.Text;

namespace ATW.MT.SystemManage
{
    public partial class UserManager : CommonPage
    {
        bus_D_user user = new bus_D_user();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
                InitList();
            }
        }
        private void BindData()
        {//数据绑定
            try
            {
                string account = txtUserAccount.Value.Trim();
                string status = ddlStatus.SelectedValue;
                DataTable dt = user.UserPagerQuery(account, status, AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize);
                if (dt != null && dt.Rows.Count > 0)
                {
                    AspNetPager1.RecordCount = Convert.ToInt32(dt.Rows[0]["totalpagesize"]);
                }
                else
                {
                    AspNetPager1.RecordCount = 0;
                }
                rpList.DataSource = dt;
                rpList.DataBind();
            }
            catch (Exception ex)
            {

            }

        }
        private void InitList()
        {
            try
            {
                ddlLevel.Items.Clear();
                DataTable dt = user.LevelQuery();
                if (dt != null && dt.Rows.Count > 0)
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        ddlLevel.Items.Add(new ListItem(dt.Rows[i]["level_class"].ToString()));
                    }
                }
            }
            catch (Exception ex)
            {

            }
        }
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindData();
        }
        protected void AspNetPager1_pageChanged(object sender, EventArgs e)
        {
            BindData();
        }
        protected void btnResetPwd_Click(object sender, EventArgs e)
        {//重置密码
            LinkButton lbn = (LinkButton)sender;
            string userid = lbn.CommandArgument;
            try
            {
                int result = user.UserResetPwd(userid);
                if (result > 0)
                {
                    Tips("密码修改成功");
                    BindData();
                }
                else
                {
                    Tips("密码修改失败");
                    return;
                }
            }
            catch (Exception ex)
            {

            }
        }
        protected void lbnBan_Click(object sender, EventArgs e)
        {//封禁
            LinkButton lbn = (LinkButton)sender;
            string userid = lbn.CommandArgument;
            try
            {
                int result = user.UserChangeStatus(userid, "2");
                if (result > 0)
                {
                    Tips("用户已封禁！");
                    BindData();
                }
                else
                {
                    Tips("操作失败");
                }
            }
            catch (Exception ex)
            {

            }
        }
        protected void lbnRecover_Click(object sender, EventArgs e)
        {//解禁
            LinkButton lbn = (LinkButton)sender;
            string userid = lbn.CommandArgument;
            try
            {
                int result = user.UserChangeStatus(userid, "1");
                if (result > 0)
                {
                    Tips("用户已解禁！");
                    BindData();
                }
                else
                {
                    Tips("操作失败");
                }

            }
            catch (Exception ex)
            {

            }
        }

        protected void lbnUpdatePwd_Click(object sender, EventArgs e)
        {
            LinkButton lbn = (LinkButton)sender;
            string account = lbn.CommandArgument;
            string url = "../BaseManage/_editPwd.aspx?uid=" + account + "&r=" + new Random().Next(1000).ToString();
            OpenDialog("修改密码", url, "height=300,width=400");
        }
        private void Tips(string s)
        {
            StringBuilder strScript = new StringBuilder();
            strScript.Append(@"<script language=javascript>
                var divTips=new jSharp.popUpDiv('divMsg');
                document.getElementById('msgContent').innerHTML='" + s + @"';
                divTips.Show();
                var t=setTimeout(function(){
                    divTips.Close();
                    clearTimeout(t);
                },1000);
            </script>");
            this.ClientScript.RegisterStartupScript(this.GetType(), "tips", strScript.ToString());
        }
    }
}